home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
398
/
398.xpi
/
chrome
/
forecastfox.jar
/
content
/
options
/
search.js
< prev
next >
Wrap
Text File
|
2010-02-04
|
8KB
|
287 lines
/*------------------------------------------------------------------------------
Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
----------------------------------------------------------------------------*/
var gSearch = null;
function searchLoad()
{
gSearch = new SearchModule();
gSearch.start();
}
function searchUnload()
{
gSearch.stop();
gSearch = null;
}
function SearchModule() {}
SearchModule.prototype = {
_baseURL: "http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=",
_bundle: null,
_textbox: null,
_results: null,
_request: null,
_running: false,
_sortId: null,
_sortDirection: null,
_naturalOrder: null,
start: function SearchModule_start()
{
this._bundle = document.getElementById("ff-bundle-search");
this._textbox = document.getElementById("ff-text-search");
this._textbox.focus();
this._textbox.select();
this._results = document.getElementById("ff-items-results");
},
stop: function SearchModule_stop()
{
this._baseURL = null;
this._bundle = null;
this._textbox = null;
this._results = null;
this._request = null;
this._running = null;
this._naturalOrder = null;
this._sortId = null;
this._sortDirection = null;
},
run: function SearchModule_run()
{
//do nothing if string not in textbox or request running
if (!this._textbox.value || this._running)
return;
//set running
this._running = true;
//remove old search
this._clear();
//set label
this._setLabel("ff.search.loading", null);
//set url
var URL = encodeURI(this._baseURL + this._textbox.value);
//setup request and send
var module = this;
this._request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
this._request.open("GET", URL, true);
this._request.overrideMimeType("text/xml; charset=iso-8859-1");
this._request.onload = function() { module._onSearchLoad(this.responseXML, this.status); };
this._request.onerror = function() { module._onSearchError(); };
try {
if (this._request.channel instanceof Components.interfaces.nsISupportsPriority)
this._request.channel.priority = Components.interfaces.nsISupportsPriority.PRIORITY_HIGHEST;
} catch(e) {}
this._request.send(null);
},
setSort: function SearchModule_setSort(aType)
{
var sortId = "ff-col-"+aType;
var column = document.getElementById(sortId);
var direction = column.getAttribute("sortDirection");
// set the sort according to the current sort direction on the column
if (direction == "ascending")
this._setSort(aType, "descending");
else if (direction == "descending")
this._setSort(aType, "natural");
else
this._setSort(aType, "ascending");
},
_setSort: function SearchModule__setSort(aType, aDirection)
{
// set the internal sort id and direction
this._sortId = "ff-col-"+aType;
this._sortDirection = aDirection;
var columns = document.getElementById("ff-tree-columns");
columns = columns.getElementsByTagName("treecol");
// update the sort attributes
for (var x = 0; x < columns.length; x++) {
if (columns[x].getAttribute("id") == this._sortId)
columns[x].setAttribute("sortDirection", aDirection);
else
columns[x].removeAttribute("sortDirection");
}
// sort the columns
this._sort();
},
_sort: function SearchModule__sort()
{
// return if there is no sorting
if (!this._sortId || !this._sortDirection) return;
var results = document.getElementById("ff-items-results");
var rows = new Array();
// get the list of rows
while (results.hasChildNodes()) {
var result = results.removeChild(results.firstChild);
if (result.nodeName == "treeitem")
rows.push(result);
}
// perform the sort based on the direction
if (this._sortDirection == "ascending")
rows.sort(sortAscending);
else if (this._sortDirection == "descending")
rows.sort(sortDescending);
else
rows = this._naturalOrder;
// then re-insert the rows
for (var x = 0; x < rows.length; x++)
results.appendChild(rows[x]);
},
_clear: function SearchModule__clear()
{
while (this._results.hasChildNodes())
this._results.removeChild(this._results.lastChild);
this._naturalOrder = new Array();
},
_setLabel: function SearchModule__setLabel(aText, aNode)
{
var treeitem, treerow, treecell1, treecell2, treecell3;
treeitem = document.createElement("treeitem");
treerow = document.createElement("treerow");
treecell1 = document.createElement("treecell");
treecell2 = document.createElement("treecell");
treecell3 = document.createElement("treecell");
if (aText) {
treeitem.setAttribute("id", "na");
treecell1.setAttribute("label", this._bundle.getString(aText));
treecell2.setAttribute("label", "");
treecell3.setAttribute("label", "");
} else {
treeitem.setAttribute("id", aNode.getAttribute("location"));
treecell1.setAttribute("label", aNode.getAttribute("city"));
treecell1.setAttribute("ref", "ff-col-city");
treecell2.setAttribute("label", aNode.getAttribute("state"));
treecell2.setAttribute("ref", "ff-col-state");
treecell3.setAttribute("label", aNode.getAttribute("location"));
treecell3.setAttribute("ref", "ff-col-id");
}
treerow.appendChild(treecell1);
treerow.appendChild(treecell2);
treerow.appendChild(treecell3);
treeitem.appendChild(treerow);
this._results.appendChild(treeitem);
this._naturalOrder.push(treeitem);
},
_onSearchLoad: function SearchModule__onSearchLoad(aResponse, aStatus)
{
//set running to false
this._running = false;
//clear old results
this._clear();
//response error
if (!aResponse || aStatus > 200) {
this._setLabel("ff.search.error", null);
return;
}
//no locations
var locs = aResponse.getElementsByTagName("location");
if (locs.length == 0) {
this._setLabel("ff.search.location", null);
return;
}
//populate tree
for (var i=0; i < locs.length; i++)
this._setLabel(null, locs[i]);
//sort the columns
this._sort();
},
_onSearchError: function SearchModule__onSearchError()
{
//set running to false
this._running = false;
//clear old results
this._clear();
//set error in tree
this._setLabel("ff.search.error", null);
},
accept: function SearchModule_accept(aClose)
{
var tree = document.getElementById("ff-tree-results");
//if nothing selected return
if (tree.currentIndex == -1) {
if (aClose) {
window.close();
return;
} else
return;
}
//if no location return
var current = tree.view.getItemAtIndex(tree.currentIndex).getAttribute("id");
if (current == "na") {
if (aClose) {
window.close();
return;
} else
return;
}
//set location value
var locid = window.opener.document.getElementById("ff-text-code");
locid.value = current;
//update apply button
window.opener.updateButtons();
window.close();
}
};
function sortDescending(aItem1, aItem2) {
var label1 = getCellForId(aItem1).getAttribute("label");
var label2 = getCellForId(aItem2).getAttribute("label");
if (label1 > label2)
return -1;
else if (label1 == label2)
return 0;
else
return 1;
}
function sortAscending(aItem1, aItem2) {
return sortDescending(aItem1, aItem2) * -1;
}
function getCellForId(aTreeitem) {
var cells = aTreeitem.getElementsByTagName("treecell");
for (var x = 0; x < cells.length; x++) {
if (cells[x].getAttribute("ref") == gSearch._sortId)
return cells[x];
}
return null;
}